package com.wenan
{
	import com.wenan.data.IDataLayer;
	import com.wenan.data.JsonDataLayer;
	import com.wenan.data.PageInfo;
	import com.wenan.data.Parameter;
	import com.wenan.data.ParameterCollection;
	import com.wenan.data.PositionType;
	import com.wenan.data.UserInfo;
	import com.wenan.ui.controls.embedBox.ReportItemBox;
	import com.wenan.ui.controls.listBoxes.TitleCheckListBox;
	import com.wenan.ui.controls.menus.PopupMenu;
	import com.wenan.ui.layout.ModulePageContainer;
	import com.wenan.ui.navigators.CountryNavigatorClientPanel;
	import com.wenan.ui.navigators.FloorNavigatorClientPanel;
	import com.wenan.ui.navigators.SquareNavigatorClientPanel;
	import com.wenan.ui.panels.LoadPanel;
	import com.wenan.ui.reports.ReportDataFactory;
	
	import mx.containers.Panel;
	import mx.controls.SWFLoader;
	import mx.rpc.events.ResultEvent;
	 
	import spark.components.Group;
	
	public class AppGlobal
	{
		public static const dataLayer:IDataLayer = new JsonDataLayer();
		
		public static var topGroup:Group = null;
		
		public static var modulePageContainer:ModulePageContainer = null;
		
		public static var reportItem:ReportItemBox = null;
		
		private static var _userInfo:UserInfo = new UserInfo();
		
		private static var _pageInfos:Array = null;
		
		private static var _homePageInfo:PageInfo = null;
		
		public static var mapTip:Panel=null;
		
		public static var swfLoader:SWFLoader = null;
		
		public static var userReportCount:Number = 1;
		
		public static var centralUrl:String;
		public static var commonUrl:String;
		public static var businessUrl:String;
		public static var pageSize:int;
		public static var UUID:String = "8a8080c933c4df220133c4e3fe600111";
		//导航信息
		public static var flag:Boolean=false; 
		public static var showChart = 1;
		
		public static var loadPanel:LoadPanel = null;
		
		public static var statisticsData:Array = null;
		/**
		 * 获取用户信息。
		 */
		public static function get userInfo():UserInfo
		{
			return _userInfo;
		}
		
		/**
		 * 设置用户信息。
		 */
		public static function set userInfo(value:UserInfo):void
		{
			if(value)
			{
				_userInfo = value;
				
				_homePageInfo = value.defaultpage;
			}
			
		}
		
		/**
		 * 获取主页信息。
		 */
		public static function get homePageInfo():PageInfo
		{
			return _homePageInfo;
		}
		
		/**
		 * 设置为主页。
		 */
		public static function set homePageInfo(value:PageInfo):void
		{
			_homePageInfo = value;
			
			dataLayer.saveHomePageInfo(userInfo.id,AppGlobal.userInfo.appkey, value,AppGlobal.pageInfos);
		}
		
		public static function setHomePageInfo(value:PageInfo):void
		{
			_homePageInfo = value;
		}
		
		/**
		 * 显示主页。
		 */
		public static function navigateHomePage():void
		{
			dataLayer.getHomePageInfo(userInfo.id,userInfo.appkey, homePageInfoResultHandler);
		}
		
		/**
		 * 主页信息从数据接口返回后的事件处理过程。
		 */
		private static function homePageInfoResultHandler(event:ResultEvent):void
		{
			//构建主页信息。
			if (event.result)
			{
				userInfo.createUserInfo(event.result);//初始化用户信息
				setHomePageInfo(userInfo.defaultpage);//初始化主页
			}
			else
			{
				var countryParameters:ParameterCollection = new ParameterCollection();
				countryParameters.add(new Parameter("navIndex","0"));
				countryParameters.add(new Parameter("navName","全国"));
				
				_homePageInfo = new PageInfo("CountryNavigator", "0", "全国", PositionType.GLOBAL,countryParameters);
			}
			
			if (_homePageInfo.pageName == "Report")
			{
				var reportDataFactory:ReportDataFactory = new ReportDataFactory();
				reportDataFactory.setReportData(_homePageInfo);
			} else
			{
				modulePageContainer.navigate(_homePageInfo, true);
			}
		}
		
		public static function get pageInfos():Array
		{
			return _pageInfos;
		}
		
		public static function set pageInfos(value:Array):void
		{
			_pageInfos = value;
		}
		
		public static function createPageInfos(pageInfos:Array):void
		{
			var navPageInfos:Array = new Array();
			for(var i:int=0;i<pageInfos.length;i++)
			{
				var pageInfo:PageInfo = new PageInfo();
				pageInfo.createPageInfo(pageInfos[i]);
				navPageInfos.push(pageInfo);
			}	
			
			
			_pageInfos = navPageInfos;
			
		}
		
		public static function destroy():void
		{
			topGroup = null;
			
			modulePageContainer = null;
			
			reportItem = null;
			
			_userInfo = new UserInfo();;
			
			_pageInfos = null;
			
			_homePageInfo = null;
			
			mapTip=null;
			
			statisticsData = null
			
		}
		
		public static function beginLoadData():void
		{
			if (loadPanel)
			{
				loadPanel.visible = true;
				loadPanel.prepare();
			}
		}
		
		public static function endLoadData():void
		{
			if (loadPanel)
			{
				loadPanel.visible = false;
			}
		}
	}
}